﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Web.Cms.Core.Configuration;
using System.Web.Mvc.Html;
using Newtonsoft.Json;
using Proggy.Web.Cms.PropertyTypes;
using Proggy.Web.Cms.Core.PropertyTypes;
using Proggy.Web.Cms.Core.Models;

namespace Proggy.Web.Cms.Core.HtmlExtensions
{
    /// <summary>
    /// Drag and drop html extensions
    /// </summary>
    public static class DragAndDropExtensions
    {
        /// <summary>
        /// Renders drag and drop area
        /// </summary>
        /// <param name="html"></param>
        /// <param name="dragAndDropContent"></param>
        public static void RenderDragAndDropArea(this HtmlHelper html, DragAndDrop dragAndDropContent, bool asEditor)
        {
            // check
            if (dragAndDropContent != null && dragAndDropContent.AddedDragAndDropItems != null && dragAndDropContent.AddedDragAndDropItems.Count() > 0) { 

            // get added parts
            var addedParts = dragAndDropContent.AddedDragAndDropItems.ToList();

            // check count
            if (addedParts.Count > 0)
            {
                // iterate
                foreach (var part in addedParts)
                {
                    // get view model
                    var viewModel = new AddedDragAndDropItemRenderViewModel
                    {
                        Item = part,
                        RenderAsEditor = asEditor
                    };

                    // render
                    RenderPartialExtensions.RenderPartial(html, "~/Areas/ProggyCore/Views/DragAndDrop/DragAndDropEditorView.cshtml", viewModel);

                    //// get config part
                    //var configPart =
                    //    DragAndDropManager.DragAndDropPartsCache.SingleOrDefault(x =>
                    //        x.Name == part.PartName);

                    //// check render
                    //if (configPart.HasAutoRenderModel && configPart.PropertyType != null) { 
                    //    // get model type data
                    //    var renderModelPropertyType = (IAutoRenderPropertyType)JsonConvert.DeserializeObject(part.ConfigData, configPart.PropertyType);

                    //    // get model 
                    //    var model = renderModelPropertyType.GetAutoRenderModel();

                    //    // render partial
                    //    RenderPartialExtensions.RenderPartial(html, renderModelPropertyType.FullPathToAutoRenderPartial, model);

                    //}
                    //else
                    //{
                    //    if (!string.IsNullOrEmpty(configPart.ContentPartial))
                    //    {
                    //        //
                    //        RenderPartialExtensions.RenderPartial(html, configPart.ContentPartial);
                    //    }
                    //}
                }
            }

            }
        }


    }
}
